package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.dto.CategoryDTO;
import com.cec.mapper.admin.ActivityMapper;
import com.cec.service.admin.ActivityService;
import com.cec.context.BaseContext;
import com.cec.dto.ActivityDTO;
import com.cec.entity.Activity;
import com.cec.exception.DataLoadingException;
import com.cec.exception.OperationDataException;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.vo.HomeDataVO;
import com.cec.vo.CategoryVO;
import com.cec.vo.ActivityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public Result pageQueryActivityList(ActivityDTO activityDTO) {
        QueryWrapper<ActivityDTO> wrapper = new QueryWrapper<>();
        if (activityDTO.getActivityName() != null)
            wrapper.like("a.activity_name", activityDTO.getActivityName());
        if (activityDTO.getStatus() != null)
            wrapper.eq("a.status", activityDTO.getStatus());
        if (activityDTO.getActivityTypeId() != null)
            wrapper.eq("a.activity_type_id", activityDTO.getActivityTypeId());
        wrapper
                .eq("a.deleted",0)
                .orderByDesc("a.create_time");
        IPage<ActivityVO> activityVOIPage = baseMapper.pageQueryActivity(new Page<>(activityDTO.getPage(), activityDTO.getPageSize()), wrapper);
        log.info("activityVOIPage.getRecords()-->{}", activityVOIPage.getRecords());
        log.info("activityVOIPage.getTotal()-->{}", activityVOIPage.getTotal());
        return Result.success(activityVOIPage.getRecords(), activityVOIPage.getTotal());

    }

    @Override
    public Result queryActivityType(CategoryDTO categoryDTO) {
        if (categoryDTO.getPage()!=0 && categoryDTO.getPageSize()!=0){
            log.info("categoryDTO.getPage()-->{}" , categoryDTO.getPage());
            log.info("categoryDTO.getPageSize()-->{}" , categoryDTO.getPageSize());
            QueryWrapper<CategoryDTO> wrapper = new QueryWrapper<>();
            if (categoryDTO.getName() != null)
                wrapper.like("type_name", categoryDTO.getName());
            IPage<CategoryVO> categoryVOIPage = baseMapper.pageQueryActivityType(new Page<>(categoryDTO.getPage(), categoryDTO.getPageSize()),wrapper);
            return Result.success(categoryVOIPage.getRecords(), categoryVOIPage.getTotal());
        }else {
            List<CategoryVO> activityCategoryVOList = baseMapper.queryActivityTypeList();
            if (activityCategoryVOList == null && activityCategoryVOList.size() <= 0)
                throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
            return Result.success(activityCategoryVOList);
        }

    }

    @Override
    public Result saveActivity(ActivityDTO activityDTO) {
        log.info("activityDTO-->{}" , activityDTO);
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        activity.setCreateUser(BaseContext.getCurrentId());
        activity.setUpdateUser(BaseContext.getCurrentId());
        log.info("activity-->{}" , activity);
        int insert = baseMapper.insert(activity);
        if (insert >0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result   updateActivity(ActivityDTO activityDTO) {
        Activity activity = baseMapper.selectById(activityDTO.getId());
        BeanUtils.copyProperties(activityDTO, activity);
        UpdateWrapper<Activity> updateWrapper = new UpdateWrapper<>();

        try {
            updateWrapper.set(activityDTO.getActivityName()!=null && !activityDTO.getActivityName().isEmpty(), "activity_name", activityDTO.getActivityName())
                    .set(activityDTO.getImg()!=null && !activityDTO.getImg().isEmpty(),"img", activityDTO.getImg())
                    .set(activityDTO.getActivityTypeId()!=null,"activity_type_id", activityDTO.getActivityTypeId())
                    .set(activityDTO.getUserName()!=null && !activityDTO.getUserName().isEmpty(),"user_name", activityDTO.getUserName())
                    .set(activityDTO.getPhone()!=null && !activityDTO.getPhone().isEmpty(),"phone", activityDTO.getPhone())
                    .set(activityDTO.getStartDate()!=null,"start_date", activityDTO.getStartDate())
                    .set(activityDTO.getEndDate()!=null,"end_date", activityDTO.getEndDate())
                    .set(activityDTO.getDetails()!=null && !activityDTO.getDetails().isEmpty(),"details", activityDTO.getDetails())
                    .set(activityDTO.getAddress()!=null && !activityDTO.getAddress().isEmpty(),"address", activityDTO.getAddress())
                    .set(activityDTO.getStatus()!=null, "status", activityDTO.getStatus())
                    .set("update_user", BaseContext.getCurrentId())
                    .eq("id", activityDTO.getId());
            baseMapper.update(new Activity(), updateWrapper);
        } catch (Exception e) {
            throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
        }
        return Result.success();
    }

    @Override
    public Activity queryActivityById(Long id) {
        Activity activity = baseMapper.selectById(id);
        if (activity == null)
            throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
        return activity;
    }

    @Override
    public Result logicDeleteActivityById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteActivityByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result getHomeActivity() {
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.groupBy("atp.type_name");
        List<HomeDataVO> homeInfoList = baseMapper.getHomeInfoData(wrapper);
        return Result.success(homeInfoList);
    }
}